﻿using System;
using System.Configuration;

namespace CN2P.Web.Controllers.Api
{
    using System.Net;
    using System.Net.Http;

    using CN2P.ServiceModel;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web.Http;

    public class ReviewsApiController : BaseApiController
    {
        private static readonly object Sync = new object();

        public static readonly List<ProductReviewModel> Reviews = new List<ProductReviewModel>();

        static ReviewsApiController()
        {
            for (var i = 0; i < 20; i++)
            {
                Reviews.Add(new ProductReviewModel
                    {
                        ReviewId = (9500 + i).ToString(),
                        Comments = string.Format("Great product! {0}", i),
                        ProdId = "101",
                        UserId = "user1@hotmail.com",
                        Rating = i % 5,
                        ReviewDate = string.Format("{0:yyyy}-{0:MM}-{0:dd} {0:hh}:{0:mm}:{0:ss}", DateTime.Now.AddDays(-i))
                    });
            }
        }

        [AcceptVerbs("GET")]
        public ProductReviewModelPagedList Get(string prodId, int pageSize = 10, int pageIndex = 0)
        {
            lock (Sync)
            {
                return null;
                //TODO: Commented right now in order to build - FIX!!!
                //var productReviews = Reviews
                //    .Where(item => item.ProdId == prodId)
                //    .OrderByDescending(item => item.ReviewDate)
                //    .ToList()
                //    .ToPagedProductReviewModel(pageIndex, pageSize);
                //return productReviews;
            }
        }

        [AcceptVerbs("GET")]
        public HttpResponseMessage GetReview(string prodId, string reviewId)
        {
            lock (Sync)
            {
                var review = Reviews
                    .FirstOrDefault(item => item.ProdId == prodId && item.ReviewId == reviewId);
                if (review == null)
                {
                    return NotFound("review-not-found", "");
                }

                return Request.CreateResponse(HttpStatusCode.OK, review);
            }
        }

        [AcceptVerbs("DELETE")]
        public HttpResponseMessage RemoveReview(string prodId, string reviewId)
        {
            lock (Sync)
            {
                var review = Reviews
                    .FirstOrDefault(item => item.ProdId == prodId && item.ReviewId == reviewId);
                if (review != null)
                {
                    Reviews.Remove(review);
                }
            }

            return Request.CreateResponse(HttpStatusCode.Created);
        }


        [AcceptVerbs("PUT")]
        public HttpResponseMessage UpdateReview(string prodId, string reviewId, ProductReviewModel model)
        {
            lock (Sync)
            {
                var review = Reviews
                    .FirstOrDefault(item => item.ProdId == prodId && item.ReviewId == reviewId);
                if (review != null)
                {
                    Reviews.Remove(review);
                }

                Reviews.Add(model);
            }

            return Request.CreateResponse(HttpStatusCode.Created);
        }

        [AcceptVerbs("POST")]
        public HttpResponseMessage Post(string prodId, ProductReviewModel review)
        {
            lock (Sync)
            {
                review.ProdId = prodId;
                review.ReviewId = Math.Abs(Guid.NewGuid().GetHashCode()).ToString();
                Reviews.Add(review);
            }

            var response = Request.CreateResponse(HttpStatusCode.Created);
            response.Headers.Location = new Uri(GetProductReviewUrl(prodId, review.ReviewId));
            return response;
        }

    }
}
